package cn.openea.perpartner.meily.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.Todo;
import cn.openea.perpartner.meily.web.entity.vo.ToDoVo;
import cn.openea.perpartner.meily.web.service.ITodoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 待办表(Todo)表控制层
 *
 * @author Weird077
 * @since 2023-06-21 10:14:45
 */
@RestController
@RequestMapping("todo")
public class TodoController extends BaseController {

    @Resource
    private ITodoService todoService;

    @PostMapping("/list")
    public TableDataInfo selectToDoList(@RequestBody Todo todo) {
        startPage();
        List<Todo> list = this.todoService.selectToDoList(todo);
        return getDataTable(list);
    }
    @PostMapping("/arc/list")
    public TableDataInfo selectDoneList(@RequestBody Todo todo) {
        startPage();
        List<Todo> list = this.todoService.selectDoneList(todo);
        return getDataTable(list);
    }
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(this.todoService.getById(id));
    }


    @PostMapping("/add")
    public AjaxResult addToDoData(@RequestBody Todo todo) {
        return success(this.todoService.addToDoData(todo));
    }


    @PostMapping("/edit")
    public AjaxResult editToDoData(@RequestBody Todo todo) {
        return success(this.todoService.updateById(todo));
    }

    @GetMapping("/read/{id}")
    public AjaxResult read(@PathVariable Long id) {
        Todo todo = Todo.builder()
                .id(id)
                .firstReadTime(DateUtil.now())
                .build();
        return todoService.updateById(todo) ? success() : error();
    }
    @PostMapping("/check/current")
    public AjaxResult checkUserTaskIsCurrentByTaskId(Todo todo) {
        return todoService.checkUserTaskIsCurrentByTaskId(todo);
    }

    /**
     * 获取待办列表-小程序
     * @param vo
     * @return
     */
    @PostMapping("/page/list")
    public TableDataInfo selectList(@RequestBody ToDoVo vo) {
        startPage();
        List<Todo> list = this.todoService.selectList(vo);
        return getDataTable(list);
    }

}

